package com.fwtai.quartz;

import com.fwtai.pay.wechat.ToolWechat;
import com.fwtai.pay.wechat.WechatPayConstants;
import com.fwtai.tool.ToolClient;
import com.fwtai.tool.ToolOkHttp;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;

@Component
@EnableScheduling
public class QuartzDelayed implements Job{

  public final static String groupName = QuartzDelayed.class.getName();//用于删除延时任务

  public final static String dateKey = "out_batch_no";//数据key的标识

  @Override
  public void execute(final JobExecutionContext context){
    final JobDetail jobDetail = context.getJobDetail();
    final JobDataMap jobDataMap = jobDetail.getJobDataMap();
    final String values = jobDataMap.getString(dateKey);
    final String[] split = values.split(",");// final String values = outTradeNo+","+transactionId+","+openid;
    final String jobName = split[0];//outTradeNo
    final String transactionId = split[1];
    final String openid = split[2];
    editDelayedResult(transactionId,openid,jobName);
  }

  public static void editDelayedResult(final String transactionId,final String openid,final String outTradeNo){
    try {
      final String accessToken = ToolWechat.getAccessToken(WechatPayConstants.APP_ID,WechatPayConstants.SECRET);
      final String upload_time = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS+08:00").format(ZonedDateTime.now());
      final HashMap<String,Object> params = new HashMap<>(6);
      final HashMap<String,Object> order_key = new HashMap<>(2);
      order_key.put("order_number_type",2);
      order_key.put("transaction_id",transactionId);
      params.put("order_key",order_key);
      params.put("logistics_type",4);
      params.put("delivery_mode",1);
      params.put("upload_time",upload_time);
      final ArrayList<HashMap<String,Object>> shipping_list = new ArrayList<>(1);
      final HashMap<String,Object> mapItem = new HashMap<>(1);
      mapItem.put("item_desc","购买商品");
      shipping_list.add(mapItem);
      final HashMap<String,Object> payer = new HashMap<>(1);
      payer.put("openid",openid);
      params.put("payer",payer);
      params.put("shipping_list",shipping_list);
      final String url = "https://api.weixin.qq.com/wxa/sec/order/upload_shipping_info?access_token="+ accessToken;
      ToolOkHttp.postAsync(url,ToolClient.toJson(params),(result)->{
        if(outTradeNo != null){
          ToolQuartz.deleteJob(outTradeNo,groupName);
        }
        /*final HashMap<String,String> map = ToolString.parseJsonObject(result);
        if("0".equals(map.get("errcode"))){//查询微信服务器端的订单状态
          final int rows = SpringContextHolder.getBean(ApiGoodsOrderService.class).editDelivery(Long.parseLong(outTradeNo),"1");//2.后加任务
          if(rows > 0){
            ToolQuartz.deleteJob(outTradeNo,groupName);//1.先删除任务
          }
        }*/
      },null);
    } catch (final Exception exception){
      exception.printStackTrace();
    }
  }
}